{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--BOOK_INFORMATION-->\n",
    "<img align=\"left\" style=\"padding-right:10px;\" src=\"fig/cover-small.jpg\">\n",
    "*This notebook contains an excerpt from the [Whirlwind Tour of Python](http://www.oreilly.com/programming/free/a-whirlwind-tour-of-python.csp) by Jake VanderPlas; the content is available [on GitHub](https://github.com/jakevdp/WhirlwindTourOfPython).*\n",
    "\n",
    "*The text and code are released under the [CC0](https://github.com/jakevdp/WhirlwindTourOfPython/blob/master/LICENSE) license; see also the companion project, the [Python Data Science Handbook](https://github.com/jakevdp/PythonDataScienceHandbook).*\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [1. Introduction](00-Introduction.ipynb) | [Contents](Index.ipynb) | [A Quick Tour of Python Language Syntax](02-Basic-Python-Syntax.ipynb) >"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# How to Run Python Code"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python is a flexible language, and there are several ways to use it depending on your particular task.\n",
    "One thing that distinguishes Python from other programming languages is that it is *interpreted* rather than *compiled*.\n",
    "This means that it is executed line by line, which allows programming to be interactive in a way that is not directly possible with compiled languages like Fortran, C, or Java. This section will describe four primary ways you can run Python code: the *Python interpreter*, the *IPython interpreter*, via *Self-contained Scripts*, or in the *Jupyter notebook*."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The Python Interpreter\n",
    "\n",
    "The most basic way to execute Python code is line by line within the *Python interpreter*.\n",
    "The Python interpreter can be started by installing the Python language (see the previous section) and typing ``python`` at the command prompt (look for the Terminal on Mac OS X and Unix/Linux systems, or the Command Prompt application in Windows):\n",
    "```\n",
    "$ python\n",
    "Python 3.5.1 |Continuum Analytics, Inc.| (default, Dec  7 2015, 11:24:55)\n",
    "Type \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n",
    ">>>\n",
    "```\n",
    "With the interpreter running, you can begin to type and execute code snippets.\n",
    "Here we'll use the interpreter as a simple calculator, performing calculations and assigning values to variables:\n",
    "``` python\n",
    ">>> 1 + 1\n",
    "2\n",
    ">>> x = 5\n",
    ">>> x * 3\n",
    "15\n",
    "```\n",
    "\n",
    "The interpreter makes it very convenient to try out small snippets of Python code and to experiment with short sequences of operations."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The IPython interpreter\n",
    "\n",
    "If you spend much time with the basic Python interpreter, you'll find that it lacks many of the features of a full-fledged interactive development environment.\n",
    "An alternative interpreter called *IPython* (for Interactive Python) is bundled with the Anaconda distribution, and includes a host of convenient enhancements to the basic Python interpreter.\n",
    "It can be started by typing ``ipython`` at the command prompt:\n",
    "```\n",
    "$ ipython\n",
    "Python 3.5.1 |Continuum Analytics, Inc.| (default, Dec  7 2015, 11:24:55) \n",
    "Type \"copyright\", \"credits\" or \"license\" for more information.\n",
    "\n",
    "IPython 4.0.0 -- An enhanced Interactive Python.\n",
    "?         -> Introduction and overview of IPython's features.\n",
    "%quickref -> Quick reference.\n",
    "help      -> Python's own help system.\n",
    "object?   -> Details about 'object', use 'object??' for extra details.\n",
    "\n",
    "In [1]: \n",
    "```\n",
    "The main aesthetic difference between the Python interpreter and the enhanced IPython interpreter lies in the command prompt: Python uses ``>>>`` by default, while IPython uses numbered commands (e.g. ``In [1]:``).\n",
    "Regardless, we can execute code line by line just as we did before:\n",
    "``` ipython\n",
    "In [1]: 1 + 1\n",
    "Out[1]: 2\n",
    "\n",
    "In [2]: x = 5\n",
    "\n",
    "In [3]: x * 3\n",
    "Out[3]: 15\n",
    "```\n",
    "Note that just as the input is numbered, the output of each command is numbered as well.\n",
    "IPython makes available a wide array of useful features; for some suggestions on where to read more, see [Resources for Further Learning](16-Further-Resources.ipynb)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Self-contained Python scripts\n",
    "\n",
    "Running Python snippets line by line is useful in some cases, but for more complicated programs it is more convenient to save code to file, and execute it all at once.\n",
    "By convention, Python scripts are saved in files with a *.py* extension.\n",
    "For example, let's create a script called *test.py* which contains the following:\n",
    "``` python\n",
    "# file: test.py\n",
    "print(\"Running test.py\")\n",
    "x = 5\n",
    "print(\"Result is\", 3 * x)\n",
    "```\n",
    "To run this file, we make sure it is in the current directory and type ``python`` *``filename``* at the command prompt:\n",
    "```\n",
    "$ python test.py\n",
    "Running test.py\n",
    "Result is 15\n",
    "```\n",
    "For more complicated programs, creating self-contained scripts like this one is a must."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### The Jupyter notebook\n",
    "\n",
    "A useful hybrid of the interactive terminal and the self-contained script is the *Jupyter notebook*, a document format that allows executable code, formatted text, graphics, and even interactive features to be combined into a single document.\n",
    "Though the notebook began as a Python-only format, it has since been made compatible with a large number of programming languages, and is now an essential part of the [*Jupyter Project*](https://jupyter.org/).\n",
    "The notebook is useful both as a development environment, and as a means of sharing work via rich computational and data-driven narratives that mix together code, figures, data, and text."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [1. Introduction](00-Introduction.ipynb) | [Contents](Index.ipynb) | [A Quick Tour of Python Language Syntax](02-Basic-Python-Syntax.ipynb) >"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
